﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Reader.Libs.PRC.Metadata
{
    public class PalmDOCHead
    {
        private byte[] compression = new byte[2];
        private byte[] unused0 = new byte[2];
        private byte[] textLength = new byte[4];
        private byte[] recordCount = new byte[2];
        public byte[] recordSize = new byte[2];
        private byte[] encryptionType = new byte[2];
        private byte[] unused1 = new byte[2];


        public void readPalmDocHead(FileStream fs)
        {
            fs.Read(this.compression, 0, this.compression.Length);
            fs.Read(this.unused0, 0, this.unused0.Length);
            fs.Read(this.textLength, 0, this.textLength.Length);
            fs.Read(this.recordCount, 0, this.recordCount.Length);

            fs.Read(this.recordSize, 0, this.recordSize.Length);
            fs.Read(this.encryptionType, 0, this.encryptionType.Length);
            fs.Read(this.unused1, 0, this.unused1.Length);
        }

        public void writePalmDocHead(FileStream fs,int t_Len, int r_Count)
        {
            this.compression = BitConverter.GetBytes((ushort)1);
            this.textLength = BitConverter.GetBytes((uint)t_Len);
            this.recordCount = BitConverter.GetBytes((ushort)r_Count);
            this.recordSize = BitConverter.GetBytes((ushort)4096);
            this.encryptionType = BitConverter.GetBytes((ushort)0);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(this.compression);
                Array.Reverse(this.textLength);
                Array.Reverse(this.recordCount);
                Array.Reverse(this.recordSize);
                Array.Reverse(this.encryptionType);
            }

            fs.Write(this.compression, 0, this.compression.Length);
            fs.Write(this.unused0, 0, this.unused0.Length);
            fs.Write(this.textLength, 0, this.textLength.Length);
            fs.Write(this.recordCount, 0, this.recordCount.Length);

            fs.Write(this.recordSize, 0, this.recordSize.Length);
            fs.Write(this.encryptionType, 0, this.encryptionType.Length);
            fs.Write(this.unused1, 0, this.unused1.Length);
        }

        //Properties
        public ushort Compression
        {
            get { return Converter.ToUInt16(this.compression); }
        }

        public string CompressionAsString
        {
            get
            {
                switch (this.Compression)
                {
                    case 1: return "None";
                    case 2: return "PalmDOC";
                    case 17480: return "HUFF/CDIC";
                    default:
                        return String.Format("Unknown (0)", this.Compression);
                }
            }
        }

        public uint TextLength
        {
            get { return Converter.ToUInt32(this.textLength); }
        }

        public ushort RecordCount
        {
            get { return Converter.ToUInt16(this.recordCount); }
        }

        public ushort RecordSize
        {
            get { return Converter.ToUInt16(this.recordSize); }
        }

        public ushort EncryptionType
        {
            get { return Converter.ToUInt16(this.encryptionType); }
        }

        public string EncryptionTypeAsString
        {
            get
            {
                switch (this.EncryptionType)
                {
                    case 0: return "None";
                    case 1: return "Old Mobipocket";
                    case 2: return "Mobipocket"; ;
                    default:
                        return String.Format("Unknown (0)", this.EncryptionType);
                }
            }
        }
    }
}
